//
//  UserManager.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/6.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "UserManager.h"

#define User_Manager    @"user_manager"

@implementation UserManager

+ (UserManager *)sharedUserManager {
    static UserManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserManager alloc] init];
        [manager initUser];
    });
    return manager;
}

- (void)initUser {
    UserModel *info = [[UserModel alloc] initWithDictionary:[UserDefaults objectForKey:User_Manager] error:nil];
    self.userInfo = info;
}

- (BOOL)isLogin {
    NSString *uid = DSStringValue([UserDefaults objectForKey:@"uid"]);
    if (uid.length > 0) {
        return YES;
    }
    return NO;
}

//退出登录
- (void)logout {
    [UserDefaults setObject:@{} forKey:User_Manager];
    [UserDefaults setObject:@"" forKey:@"avatarUrl"];
    [UserDefaults setObject:@"" forKey:@"mobile"];
    [UserDefaults setObject:@"" forKey:@"nickName"];
    [UserDefaults setObject:@"" forKey:@"uid"];
    self.userInfo = nil;
    
    [UserDefaults synchronize];
}

//登录
- (void)updateUserInfo:(NSDictionary *)uInfo {
    self.userInfo = [[UserModel alloc] initWithDictionary:uInfo error:nil];

    [UserDefaults setObject:uInfo forKey:User_Manager];
    [UserDefaults setObject:uInfo[@"avatarUrl"] forKey:@"avatarUrl"];
    [UserDefaults setObject:uInfo[@"mobile"] forKey:@"mobile"];
    [UserDefaults setObject:uInfo[@"nickName"] forKey:@"nickName"];
    [UserDefaults setObject:uInfo[@"uid"] forKey:@"uid"];
    [UserDefaults synchronize];
    
    
}

- (void)saveImageDocuments:(UIImage *)image {
    UIImage *imagesave = image;
    NSString *path_sandox = NSHomeDirectory();
    //设置一个图片的存储路径
    NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/test.png"];
    //把图片直接保存到指定的路径
    [UIImagePNGRepresentation(imagesave) writeToFile:imagePath atomically:YES];
}


- (UIImage *)getDocumentImage {
    // 读取沙盒路径图片
    NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.png",NSHomeDirectory(),@"test"];
    // 拿到沙盒路径图片
    UIImage *img=[[UIImage alloc]initWithContentsOfFile:aPath3];
    //返回该图片
    return img;
}


@end
